package com.charging.business.media;

import com.charging.business.utils.ToolsUtil;

/**
 * CPC卡CPC卡过站信息文件(DF/EF02)的编码信息
 */
public class CpcEf02EdModel {
    public static final int MSG_LEN = 101;

    /**
     * 通行省份个数(01-01,1,n)
     * 入口车道置1，经过省界入口ETC门架时累加
     */
    public byte[] provCount = new byte[1];

    /**
     * 本省门架个数(02-02,1,n)即当前通行省份内所有途径ETC门架个数
     */
    public byte[] gantryCount = new byte[1];

    /**
     * 本省累计金额(03-05,3,n)当前通行省份内所有途径ETC门架（ 含拟合门架）的累计计费金额
     */
    public byte[] provSumPayFee = new byte[3];

    /**
     * 本省计费里程(06-08,3,n)当前通行省份内所有途径ETC门架（ 含拟合） 对应的累计计费里程， 单位米
     */
    public byte[] sumMileage = new byte[3];

    /**
     * 本省入口ETC门架信息(09-11,3,bn)
     * 省界入口ETC门架或省内第一个ETC门架的信息<br>
     * 3字节ETC门架编码 ，入口收费站3字节ETC门架编码填入0x00
     */
    public byte[] provEntryHex = new byte[3];

    /**
     * 本省入口时间(12-15,4,n)UNIX时间
     */
    public byte[] enTime = new byte[4];

    /**
     * 最新过门架编码(16-18,3,bn)
     */
    public byte[] newGantryNo = new byte[3];

    /**
     * 最新过门架时间(19-22,4,n) UNIX时间
     */
    public byte[] newGantryTime = new byte[4];

    /**
     * 最新过门架计费金额(23-25,3,n)
     */
    public byte[] newGantryAmount = new byte[3];

    /**
     * 最新过门架计费里程(26-28,3,n)
     */
    public byte[] newGantryMileage = new byte[3];

    /**
     * 过站信息中写入的门架数量(29-29,1,n)
     */
    public byte[] gantryNum = new byte[1];

    /**
     * 过站门架HEXS(30-101,72,bn)
     * 3字节门架编号,共计可记录24个门架
     */
    public byte[][] gantryHexs = new byte[24][3];

    public static CpcEf02EdModel parseSelf(byte[] buff) {
        int index = 0;
        CpcEf02EdModel ret = new CpcEf02EdModel();
        index += ToolsUtil.arrcopy(buff, index, ret.provCount);
        index += ToolsUtil.arrcopy(buff, index, ret.gantryCount);
        index += ToolsUtil.arrcopy(buff, index, ret.provSumPayFee);
        index += ToolsUtil.arrcopy(buff, index, ret.sumMileage);
        index += ToolsUtil.arrcopy(buff, index, ret.provEntryHex);
        index += ToolsUtil.arrcopy(buff, index, ret.enTime);
        index += ToolsUtil.arrcopy(buff, index, ret.newGantryNo);
        index += ToolsUtil.arrcopy(buff, index, ret.newGantryTime);
        index += ToolsUtil.arrcopy(buff, index, ret.newGantryAmount);
        index += ToolsUtil.arrcopy(buff, index, ret.newGantryMileage);
        index += ToolsUtil.arrcopy(buff, index, ret.gantryNum);

        for (byte[] sub : ret.gantryHexs) {
            index += ToolsUtil.arrcopy(buff, index, sub);
        }

        return ret;
    }

    public static CpcEf02EdModel parse(String strHex) {
        if (strHex == null || strHex.isEmpty()) {
            return null;
        }
//		if(strHex.length()<CPCDFEF02ED.MSG_LEN*2)
//			return null;
        if (!ToolsUtil.chkHexChar(strHex)) {
            return null;
        }
        byte[] buff = ToolsUtil.hex2bin(strHex);
        return parseSelf(buff);
    }

    /**
     * 信息解码
     *
     * @return
     */
    public CpcEf02Model decoded() {
        CpcEf02Model dd = new CpcEf02Model();
        dd.provCount = ToolsUtil.bin2int(provCount);
        dd.gantryCount = ToolsUtil.bin2int(gantryCount);
        dd.provSumPayFee = ToolsUtil.bin2int(provSumPayFee);
        dd.sumMileage = ToolsUtil.bin2int(sumMileage);
        dd.provEntryHex = ToolsUtil.bin2hex(provEntryHex);
        dd.enTime = ToolsUtil.bin2lng(enTime);
        dd.newGantryNo = ToolsUtil.bin2hex(newGantryNo);
        dd.newGantryTime = ToolsUtil.bin2lng(newGantryTime);
        dd.newGantryAmount = ToolsUtil.bin2int(newGantryAmount);
        dd.newGantryMileage = ToolsUtil.bin2int(newGantryMileage);
        dd.gantryNum = ToolsUtil.bin2int(gantryNum);

        for (int aa = 0; aa < dd.gantryNum && aa < gantryHexs.length; aa++) {
            dd.gantryHexs[aa] = ToolsUtil.bin2hex(gantryHexs[aa]);
        }

        return dd;
    }
}
